package com.wp.service.impl;

import com.wp.mapper.CommentMapper;
import com.wp.mapper.UserMapper;
import com.wp.pojo.Comment;
import com.wp.pojo.PageResult;
import com.wp.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 获取评论列表-----每次都会查询数据库----效率低
     * */
    @Override
    public PageResult getCommentList(PageResult pageResult){
        Integer status = 1;
        String query = pageResult.getQuery();
        Long total = commentMapper.getCommentCount(query,status);
        Integer size=pageResult.getPageSize();
        Integer num =pageResult.getPageNum();
        Integer startNum=(num-1) * size;
        List<Comment> comments = commentMapper.getCommentList(query,startNum,size,status);
        //遍历获取用户的姓名和被评论的用户名字
        for (Comment comment: comments) {
            String toUserUid= comment.getToUserUid();
            String userUid = comment.getUserUid();
            String userName = userMapper.getUserName(userUid);
            String toUserName = userMapper.getToUserName(toUserUid);
            comment.setUserName(userName).setToUserName(toUserName);
        }
        pageResult.setTotal(total);
        pageResult.setRows(comments);
        return pageResult;
    }


//    public PageResult getCommentList(PageResult pageResult) {
//        pageResult.setStatus(1);
//        String query = pageResult.getQuery();
//        Integer status = pageResult.getStatus();
//        Long total = commentMapper.getCommentCount(query,status);
//        Integer size=pageResult.getPageSize();
//        Integer num =pageResult.getPageNum();
//        Integer startNum=(num-1) * size;
//        List<Comment> comments= commentMapper.getCommentList(query,startNum,size,status);
//        pageResult.setTotal(total);
//        pageResult.setRows(comments);
//        return pageResult;
//    }
    /**
     * 删除评论
     * */
//    @Override
//    public void deleteComment(String uid) {
//        Date updateTime = new Date();
//        commentMapper.deleteComment(uid,updateTime);
//    }
    @Override
    public void deleteComment(Comment comment) {
        String uid = comment.getUid();
        Integer status = 0;
        Date date = new Date();
        Date updatedTime = date;
        commentMapper.deleteComment(uid,updatedTime,status);
    }

}
